package com.ll.menu.user.food.list;

import android.graphics.Paint;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.droid.library.app.App;
import com.droid.library.thirdplatform.imageloader.CoreImageLoader;
import com.droid.library.utils.app.DeviceUtils;
import com.droid.library.utils.app.PixelUtil;
import com.droid.library.widget.recyclerview.BaseRecyclerAdapter;
import com.droid.library.widget.recyclerview.BaseViewHolder;
import com.ll.menu.user.R;
import com.ll.menu.user.app.entity.Food;
import com.ll.menu.user.food.detail.FoodDetailActivity;

/**
 * Food
 *
 * @author wangheng
 */

public class FoodListAdapter extends BaseRecyclerAdapter<Food> {

    public FoodListAdapter(Fragment fragment) {
        super(fragment);
    }

    @Override
    protected BaseViewHolder onCreateRealViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = mActivity.getLayoutInflater();
        View view = inflater.inflate(R.layout.item_food_list,parent,false);
        return new Holder(view,mFragment,FoodListAdapter.this);
    }

    private class Holder extends BaseViewHolder<Food>{

        private ImageView mImageView;
        private TextView mCountdownTextView;
        private TextView mNameTextView;
        private TextView mCurrentPriceTextView;
        private TextView mOldPriceTextView;
        private TextView mPickTextView;



        public Holder(final View itemView, Fragment fragment, BaseRecyclerAdapter<Food> adapter) {
            super(itemView, fragment, adapter);

            mImageView = (ImageView) itemView.findViewById(R.id.ivFoodItemImage);
            mCountdownTextView = (TextView) itemView.findViewById(R.id.tvFoodItemCountdown);
            mNameTextView = (TextView) itemView.findViewById(R.id.tvFoodItemName);
            mCurrentPriceTextView = (TextView) itemView.findViewById(R.id.tvFoodItemCurrentPrice);
            mOldPriceTextView = (TextView) itemView.findViewById(R.id.tvFoodItemOldPrice);
            mPickTextView = (TextView) itemView.findViewById(R.id.tvFoodItemPick);

            mOldPriceTextView.getPaint().setFlags(mOldPriceTextView.getPaintFlags()
                    | Paint.STRIKE_THRU_TEXT_FLAG);

            itemView.post(new Runnable() {
                @Override
                public void run() {
                    int screenWidth = DeviceUtils.getScreenWidth(App.getInstance().getContext());
                    float width = screenWidth * 5 / 6.0f;
                    int size = (int) ((width - 4 * PixelUtil.dip2px(12)) / 3);
                    ViewGroup.LayoutParams lp = itemView.getLayoutParams();
                    lp.width = size;
                    lp.height = size;
                    itemView.setLayoutParams(lp);
                }
            });
        }

        @Override
        public void onBindViewHolder(int position, Food item) {
            CoreImageLoader.getInstance().display(mImageView,item.getImage(),R.drawable.default_image);
            mCountdownTextView.setText(String.valueOf(position * position + 10));
            mNameTextView.setText(item.getName());
            mCurrentPriceTextView.setText(String.valueOf(item.getCurrentPrice()));
            if(item.getOldPrice() <= 0) {
                mOldPriceTextView.setVisibility(View.INVISIBLE);
            }else{
                mOldPriceTextView.setVisibility(View.VISIBLE);
                mOldPriceTextView.setText(String.valueOf(item.getOldPrice()));
            }

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    FoodDetailActivity.launch(mActivity);
                }
            });

            mPickTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
        }
    }
}
